package com.wish.g1.accounting.controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.g1.accounting.entity.Balance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.g1.common.log.annotation.Log;
import com.wish.g1.common.log.enums.BusinessType;
import com.wish.g1.common.security.annotation.RequiresPermissions;
import com.wish.g1.accounting.service.IBalanceService;
import com.wish.g1.common.core.web.controller.BaseController;
import com.wish.g1.common.core.web.domain.AjaxResult;
import com.wish.g1.common.core.utils.poi.ExcelUtil;
import com.wish.g1.common.core.web.page.TableDataInfo;

/**
 * 企业账户流水明细(企业账户变动历史记录)Controller
 *
 * @author wish
 * @date 2022-11-21
 */
@RestController
@RequestMapping("/balance")
public class BalanceController extends BaseController
{
    @Autowired
    private IBalanceService balanceService;

    /**
     * 查询企业账户流水明细(企业账户变动历史记录)列表
     */
    @RequiresPermissions("accounting:balance:list")
    @GetMapping("/list")
    public TableDataInfo list(Balance balance)
    {
        startPage();
        QueryWrapper wrapper=new QueryWrapper();
        if (balance.getBusinessId() !=null){
            wrapper.eq("business_id",balance.getBusinessId());
        }
        if (StringUtils.hasText(balance.getBusinessName())) {
            wrapper.like("business_name", balance.getBusinessName());
        }
        List<Balance> list = balanceService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 导出企业账户流水明细(企业账户变动历史记录)列表
     */
    @RequiresPermissions("accounting:balance:export")
    @Log(title = "企业账户流水明细(企业账户变动历史记录)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Balance balance)
    {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.setEntity(balance);
        List<Balance> list = balanceService.list(wrapper);
        ExcelUtil<Balance> util = new ExcelUtil<Balance>(Balance.class);
        util.exportExcel(response, list, "企业账户流水明细(企业账户变动历史记录)数据");
    }

    /**
     * 获取企业账户流水明细(企业账户变动历史记录)详细信息
     */
    @RequiresPermissions("accounting:balance:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(balanceService.getById(id));
    }

    /**
     * 新增企业账户流水明细(企业账户变动历史记录)
     */
    @RequiresPermissions("accounting:balance:add")
    @Log(title = "企业账户流水明细(企业账户变动历史记录)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Balance balance)
    {
        System.err.println("accounting:balance:add");
        balance.setCreateTime(LocalDateTime.now());
        return toAjax(balanceService.save(balance));
    }

    /**
     * 修改企业账户流水明细(企业账户变动历史记录)
     */
    @RequiresPermissions("accounting:balance:edit")
    @Log(title = "企业账户流水明细(企业账户变动历史记录)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Balance balance)
    {
        System.err.println(balance);
        balance.setUpdateTime(LocalDateTime.now());
        return toAjax(balanceService.saveOrUpdate(balance));
    }

    /**
     * 删除企业账户流水明细(企业账户变动历史记录)
     */
    @RequiresPermissions("accounting:balance:remove")
    @Log(title = "企业账户流水明细(企业账户变动历史记录)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(balanceService.removeByIds(Arrays.asList(ids)));
    }
}
